<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="发布人ID" prop="postedMemberId">
        <el-input
          v-model="queryParams.postedMemberId"
          placeholder="请输入发布人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="发布人" prop="postedMemberId">
        <el-select v-model="queryParams.postedMemberId" placeholder="请选择" clearable filterable>
          <el-option
            v-for="item in memberMap"
            :key="item.userId"
            :label="item.memberNickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务标题" prop="postedTitle">
        <el-input
          v-model="queryParams.postedTitle"
          placeholder="请输入任务标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="postedStatus">
        <!-- <el-input
          v-model="queryParams.contactIntroCode"
          placeholder="请输入介绍人"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.postedStatus" placeholder="请选择" clearable>
          <el-option
            v-for="item in dict.type.reward_task_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属地区" prop="postedRegion">
        <!-- <el-input
          v-model="queryParams.postedRegion"
          placeholder="请输入所属地区"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          v-model="queryParams.postedRegion"
          :options="regions"
          :props="{
            value: 'code',
            label: 'name',
            children:'childs'
          }"
          @change="handleRegionsParamChange"></el-cascader>
      </el-form-item>
      <el-form-item label="任务金额" prop="postedAmount">
        <el-input
          v-model="queryParams.postedAmount"
          placeholder="请输入任务金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金比例" prop="postedCommissionRate">
        <el-input
          v-model="queryParams.postedCommissionRate"
          placeholder="请输入佣金比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示时间" prop="postedDisplayDuration">
        <!-- <el-input
          v-model="queryParams.postedDisplayDuration"
          placeholder="请输入显示时间"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.postedDisplayDuration" placeholder="显示时间" clearable>
          <el-option
            v-for="dict in dict.type.reward_show_day"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value * 1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="承接人" prop="postedDepositId">
        <!-- <el-input
          v-model="queryParams.postedDepositId"
          placeholder="请输入承接人"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.postedDepositId" placeholder="请选择" clearable filterable>
          <el-option
            v-for="item in memberMap"
            :key="item.userId"
            :label="item.memberNickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="postedTime">
        <el-date-picker clearable
          v-model="queryParams.postedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reward:posted:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reward:posted:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reward:posted:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reward:posted:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="任务名称" align="center" prop="postedTitle" width="190" />
      <el-table-column label="发布人" align="center" prop="postedNickName" />
      <el-table-column label="所属地区" align="center" prop="postedRegion" />
      <el-table-column label="任务金额" align="center" prop="postedAmount" />
      <el-table-column label="佣金类型" align="center" prop="postedCommissionType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reward_commission_type" :value="scope.row.postedCommissionType"/>
        </template>
      </el-table-column>
      <el-table-column label="佣金金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.postedCommissionType === '0'?scope.row.postedCommissionAmount:scope.row.postedCommissionRate }}
        </template>
      </el-table-column>
      
      <!-- <el-table-column label="佣金比例" align="center" prop="postedCommissionRate" />
      <el-table-column label="佣金金额" align="center" prop="postedCommissionAmount" /> -->
      <el-table-column label="要求完成时间" align="center" prop="postedRequiredTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.postedRequiredTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人押金" align="center" prop="postedPay" width="100" />
      <el-table-column label="承接人押金" align="center" prop="postedDeposit" width="100" />
      <el-table-column label="地区" align="center" prop="regionAllName" width="190" />
      <!-- reward_show_day -->
      <el-table-column label="显示时间" align="center" prop="postedDisplayDuration">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reward_show_day" :value="scope.row.postedDisplayDuration"/>
        </template>
      </el-table-column>
      <el-table-column label="任务内容" align="center" prop="postedDescription" />
      <el-table-column label="图片" align="center" prop="postedImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.postedImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="承接人" align="center" prop="postedDepositId" />
      <el-table-column label="发布时间" align="center" prop="postedTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.postedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="postedStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reward_task_status" :value="scope.row.postedStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="排序顺序" align="center" prop="sortOrder" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-reading"
            @click="openDetailHandle(scope.row)"
          >详情</el-button>
          <!--  v-if="scope.row.postedStatus === '0'" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reward:posted:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reward:posted:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.postedStatus === '0'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="openCheckVisible(scope.row)"
          >审核</el-button>
          <el-button
            v-if="scope.row.assigneeCount > 0 && scope.row.postedStatus === '1'"
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="openReceptVisible(scope.row)"
          >承接人选择</el-button>
          <el-button
          v-if="scope.row.postedStatus === '3'"
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="openShareMoneyVisible(scope.row)"
          >分润</el-button>
          <el-button
            v-if="scope.row.postedStatus === '3'"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="openShareMoneyListVisible(scope.row)"
          >分润列表</el-button>
          <el-button
            v-if="scope.row.postedStatus === '3'"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="endTask(scope.row)"
          >结束任务</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改悬赏任务发布对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="发布人ID" prop="postedMemberId">
              <el-input v-model="form.postedMemberId" placeholder="请输入发布人ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="postedTitle">
              <el-input v-model="form.postedTitle" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务金额" prop="postedAmount">
              <el-input v-model="form.postedAmount" placeholder="请输入任务金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属地区" prop="postedRegion">
              <!-- <el-input v-model="form.postedRegion" placeholder="请输入所属地区" /> -->
              <el-cascader
                ref="regionsRef"
                v-model="form.postedRegion"
                :options="regions"
                :props="{
                  value: 'code',
                  label: 'name',
                  children:'childs'
                }"
                @change="handleRegionsChange"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="佣金类型" prop="postedCommissionType">
              
                <el-radio-group v-model="form.postedCommissionType">
                  <el-radio :label="dict.value" v-for="dict in dict.type.reward_commission_type" :key="dict.value">{{ dict.label }}</el-radio>
                  <!-- <el-radio :label="6">备选项</el-radio>
                  <el-radio :label="9">备选项</el-radio> -->
                </el-radio-group>
              <!-- <el-select v-model="form.postedCommissionType" placeholder="佣金类型" clearable>
                <el-option
                  v-for="dict in dict.type.reward_commission_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.postedCommissionType === '1'">
            <el-form-item label="佣金比例" prop="postedCommissionRate">
              <el-input v-model="form.postedCommissionRate" placeholder="请输入佣金比例" />
            </el-form-item>
          </el-col>
          <el-col :span="12"  v-else>
            <el-form-item label="佣金金额" prop="postedCommissionAmount">
              <el-input v-model="form.postedCommissionAmount" placeholder="佣金金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- reward_show_day -->
            <el-form-item label="显示时间" prop="postedDisplayDuration">
              <!-- <el-input v-model="form.postedDisplayDuration" placeholder="请输入显示时间" /> -->
              <el-select v-model="form.postedDisplayDuration" placeholder="显示时间" clearable>
                <el-option
                  v-for="dict in dict.type.reward_show_day"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value * 1"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="要求完成时间" prop="postedRequiredTime" label-width="120px">
              <el-date-picker clearable
                v-model="form.postedRequiredTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择要求完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序顺序" prop="sortOrder">
              <el-input v-model="form.sortOrder" placeholder="请输入排序顺序" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承接人押金" prop="postedDeposit">
              <el-input v-model="form.postedDeposit" placeholder="请输入承接人押金" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务内容" prop="postedDescription">
              <el-input v-model="form.postedDescription" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="postedImage">
              <image-upload v-model="form.postedImage"/>
            </el-form-item>
          </el-col>
          
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <detail :visible.sync="openDetail" :record="detailInfo" />
    <check :visible.sync="openCheck" :record="checkInfo" @cancelHandle="getList" />
    <receptTask :id="receptId" :visible.sync="receptTaskVisible" @cancelHandle="getList" />
    <shareMoney :id="receptId" :visible.sync="shareVisible" @cancelHandle="getList"></shareMoney>
    <share-money-list :id="receptId" :visible.sync="shareMoneyListVisible"></share-money-list>
  </div>
</template>

<script>
import { listPosted, getPosted, delPosted, addPosted, updatePosted } from "@/api/reward/posted";
import { memberKeyMap } from "@/api/center/member"
import detail from "./components/detail.vue";
import check from "./components/check.vue";
import receptTask from "./components/receptTask.vue";
import shareMoney from "./components/shareMoney.vue";
import shareMoneyList from "./components/shareMoneyList.vue";
import regions from "@/utils/regions.json"
import { checkPosted } from "@/api/reward/posted";
export default {
  name: "Posted",
  dicts: ['reward_show_day', 'reward_task_status', 'reward_commission_type'],
  components: {
    detail,
    check,
    receptTask,
    shareMoney,
    shareMoneyList
  },
  data() {
    return {
      // 打开分润列表
      shareMoneyListVisible: false,
      // 打开分润表单
      shareVisible: false,
      // 打开弹窗id
      receptId:'',
      // 选择承接人
      receptTaskVisible: false,
      regions:regions,
      // 打开审核
      openCheck: false,
      // 审核详情
      checkInfo: {},
      // 打开详情
      openDetail: false,
      // 详情信息
      detailInfo: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 悬赏任务发布表格数据
      postedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postedMemberId: null,
        postedTitle: null,
        postedRegion: null,
        postedAmount: null,
        postedCommissionType: null,
        postedCommissionRate: null,
        postedCommissionAmount: null,
        postedRequiredTime: null,
        postedDeposit: null,
        postedDisplayDuration: null,
        postedDescription: null,
        postedImage: null,
        postedOrigin: null,
        postedOriginId: null,
        postedDepositId: null,
        postedTime: null,
        postedStatus: null,
        sortOrder: null,
        createdAt: null,
        updatedAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postedMemberId: [
          { required: true, message: "发布人ID不能为空", trigger: "blur" }
        ],
        postedTitle: [
          { required: true, message: "任务标题不能为空", trigger: "blur" }
        ],
        postedRegion: [
          { required: true, message: "所属地区不能为空", trigger: "blur" }
        ],
        postedCommissionType: [
          { required: true, message: "佣金类型不能为空", trigger: "change" }
        ],
        postedRequiredTime: [
          { required: true, message: "要求完成时间不能为空", trigger: "blur" }
        ],
        sortOrder: [
          { required: true, message: "排序顺序不能为空", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        postedDescription: [
          { required: true, message: "请输入任务内容", trigger: "blur" }
        ],
      },
      // 会员列表
      memberMap: []
    };
  },
  created() {
    this.getMemberMap()
    this.getList();
  },
  methods: {
    // 获取会员列表
    getMemberMap() {
      memberKeyMap().then(res => {
        const { code, data } = res
        if (code === 200) {
          this.memberMap = data || []
        }
      })
    },
    // 打开分润列表
    openShareMoneyListVisible(record) {
      this.receptId = record.postedId
      this.shareMoneyListVisible = true
    },
    // 打开分润窗口
    openShareMoneyVisible(record) {
      this.receptId = record.postedId
      this.shareVisible = true
    },
    // 打开选择承接人弹窗
    openReceptVisible(record) {
      this.receptId = record.postedId
      this.receptTaskVisible = true
    },
     // 打开审核
     openCheckVisible(record) {
      this.openCheck = true
      this.checkInfo = record
    },
    // 打开详情
    openDetailHandle(record) {
      this.detailInfo = record
      this.openDetail = true
    },
    /** 查询悬赏任务发布列表 */
    getList() {
      this.loading = true;
      listPosted(this.queryParams).then(response => {
        this.postedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postedId: null,
        postedMemberId: null,
        postedTitle: null,
        postedRegion: '410102',
        postedAmount: null,
        postedCommissionType: '0',
        postedCommissionRate: null,
        postedCommissionAmount: null,
        postedRequiredTime: null,
        postedDeposit: null,
        postedDisplayDuration: null,
        postedDescription: null,
        postedImage: null,
        postedOrigin: null,
        postedOriginId: null,
        postedDepositId: null,
        postedTime: null,
        postedStatus: null,
        sortOrder: 50,
        createdAt: null,
        updatedAt: null,
        updatedBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postedId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加悬赏任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postedId = row.postedId || this.ids
      getPosted(postedId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改悬赏任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.postedId != null) {
            updatePosted(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPosted(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postedIds = row.postedId || this.ids;
      this.$modal.confirm('是否确认删除悬赏任务发布编号为"' + postedIds + '"的数据项？').then(function() {
        return delPosted(postedIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reward/posted/export', {
        ...this.queryParams
      }, `posted_${new Date().getTime()}.xlsx`)
    },
    // 选择地区
    handleRegionsChange(e) {
      if (e.length > 0) {
        this.form.postedRegion = e[e.length - 1]
        const regionCheckNode = this.$refs.regionsRef.getCheckedNodes()
        this.form.regionAllName = regionCheckNode[0].pathLabels.join(',')
      } else {
        this.form.postedRegion = ''
        this.form.regionAllName = ''
      }
    },
    handleRegionsParamChange(e) {
      if (e.length > 0) {
        this.queryParams.postedRegion = e[e.length - 1]
      } else {
        this.queryParams.postedRegion = ''
      }
    },
    // 结束任务
    endTask(record) {
      const postData = {
        postedId: record.postedId,
        postedStatus: 2,
      }
      this.$modal.confirm('是否确认结束任务名称为"' + record.postedTitle + '"的数据项？').then(function() {
        return checkPosted(postData);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("任务已结束");
      }).catch(() => {});
    }
  }
};
</script>
